package com.small.xx.ai.file.dto;

import java.util.List;
import java.util.ArrayList;

/**
 * 多文件上传响应
 * 
 * @author xx-ai
 * @version 1.0.0
 */
public class MultiFileUploadResponse {
    
    /**
     * 是否成功
     */
    private Boolean success;
    
    /**
     * 响应消息
     */
    private String message;
    
    /**
     * 总文件数
     */
    private Integer totalFiles;
    
    /**
     * 成功上传文件数
     */
    private Integer successCount;
    
    /**
     * 失败文件数
     */
    private Integer failCount;
    
    /**
     * 每个文件的上传结果
     */
    private List<FileUploadResponse> results;
    
    /**
     * 构造函数
     */
    public MultiFileUploadResponse() {
        this.results = new ArrayList<>();
        this.success = false;
        this.totalFiles = 0;
        this.successCount = 0;
        this.failCount = 0;
    }
    
    /**
     * 成功响应构造函数
     * 
     * @param message 消息
     * @param results 上传结果列表
     * @return 成功响应
     */
    public static MultiFileUploadResponse success(String message, List<FileUploadResponse> results) {
        MultiFileUploadResponse response = new MultiFileUploadResponse();
        response.setSuccess(true);
        response.setMessage(message);
        response.setResults(results);
        response.setTotalFiles(results.size());
        
        long successCount = results.stream().mapToLong(r -> r.getSuccess() ? 1 : 0).sum();
        response.setSuccessCount((int) successCount);
        response.setFailCount(results.size() - (int) successCount);
        
        return response;
    }
    
    /**
     * 错误响应构造函数
     * 
     * @param message 错误消息
     * @param totalFiles 总文件数
     * @return 错误响应
     */
    public static MultiFileUploadResponse error(String message, int totalFiles) {
        MultiFileUploadResponse response = new MultiFileUploadResponse();
        response.setSuccess(false);
        response.setMessage(message);
        response.setTotalFiles(totalFiles);
        response.setSuccessCount(0);
        response.setFailCount(totalFiles);
        return response;
    }
    
    // Getters and Setters
    
    public Boolean getSuccess() {
        return success;
    }
    
    public void setSuccess(Boolean success) {
        this.success = success;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public Integer getTotalFiles() {
        return totalFiles;
    }
    
    public void setTotalFiles(Integer totalFiles) {
        this.totalFiles = totalFiles;
    }
    
    public Integer getSuccessCount() {
        return successCount;
    }
    
    public void setSuccessCount(Integer successCount) {
        this.successCount = successCount;
    }
    
    public Integer getFailCount() {
        return failCount;
    }
    
    public void setFailCount(Integer failCount) {
        this.failCount = failCount;
    }
    
    public List<FileUploadResponse> getResults() {
        return results;
    }
    
    public void setResults(List<FileUploadResponse> results) {
        this.results = results;
    }
    
    /**
     * 添加单个文件上传结果
     * 
     * @param result 单个文件上传结果
     */
    public void addResult(FileUploadResponse result) {
        if (this.results == null) {
            this.results = new ArrayList<>();
        }
        this.results.add(result);
        
        // 更新统计信息
        this.totalFiles = this.results.size();
        this.successCount = (int) this.results.stream().mapToLong(r -> r.getSuccess() ? 1 : 0).sum();
        this.failCount = this.totalFiles - this.successCount;
        
        // 更新整体成功状态
        this.success = this.successCount > 0;
    }
    
    @Override
    public String toString() {
        return "MultiFileUploadResponse{" +
                "success=" + success +
                ", message='" + message + '\'' +
                ", totalFiles=" + totalFiles +
                ", successCount=" + successCount +
                ", failCount=" + failCount +
                ", results=" + results +
                '}';
    }
}